package com.clayecloud.common.client.services.Impl;

import com.clayecloud.common.client.entity.ClientUserBindHouse;
import com.clayecloud.common.entity.House;
import com.clayecloud.common.mappers.HouseMapper;
import com.clayecloud.common.utils.MessageUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.clayecloud.common.mappers.HouseUserMapper;
import com.clayecloud.common.entity.HouseUser;
import com.clayecloud.common.client.services.HouseUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author  ClayeLink
 * @date  2021/12/31 14:51
 */
@Service
public class HouseUserServiceImpl implements HouseUserService{

    @Resource
    private HouseUserMapper houseUserMapper;

    @Resource
    private HouseMapper houseMapper;

    @Override
    public MessageUtil deleteByPrimaryKey(Integer houseId,Integer userId) {
        return  houseMapper.deleteByPrimaryKey(houseId) > 0 && houseUserMapper.deleteByPrimaryKeyAndUserID(houseId,userId) > 0 ? MessageUtil.success("解绑成功") : MessageUtil.fail("解绑失败");
    }

    @Override
    public int insert(HouseUser record) {
        return houseUserMapper.insert(record);
    }

    @Override
    public int insertSelective(HouseUser record) {
        return houseUserMapper.insertSelective(record);
    }

    @Override
    public HouseUser selectByPrimaryKey(Integer houseId,Integer userId) {
        return houseUserMapper.selectByPrimaryKey(houseId,userId);
    }

    @Override
    public int updateByPrimaryKeySelective(HouseUser record) {
        return houseUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(HouseUser record) {
        return houseUserMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<HouseUser> selectByUserID(Integer userId) {
        return houseUserMapper.selectByUserID(userId);
    }

    @Override
    public List<HouseUser> selectByHouseID(Integer houseId) {
        return houseUserMapper.selectByUserID(houseId);
    }

    @Override
    public MessageUtil checkUserBindHouse(Integer userID) {
        List<HouseUser> houseUsers = houseUserMapper.selectByUserID(userID);
        if(houseUsers.size() != 0){
           return MessageUtil.success("查询成功").add(true);
        }
        System.out.println(userID);
        return MessageUtil.success("查询成功").add(false);
    }

    @Override
    public MessageUtil getUserBindAllHouse(Integer userID) {
        List<HouseUser> houseUsers = houseUserMapper.selectByUserID(userID);
        List<House> houses = new ArrayList<>();
        for(HouseUser houseUser : houseUsers){
            House house = houseMapper.selectByPrimaryKey(houseUser.getHouseId());
            houses.add(house);
        }
        return MessageUtil.success("查询成功").add(houses);
    }

    @Override
    public MessageUtil userBindHouse(ClientUserBindHouse clientUserBindHouse) {
            House house = new House();
            house.setHouseAddrass(clientUserBindHouse.getHouseAddrass());
            house.setHouseResidentialQuarters(clientUserBindHouse.getHouseResidentialQuarters());
            house.setHouseNumber(clientUserBindHouse.getHouseNumber());
            houseMapper.insert(house);
            HouseUser houseUser = new HouseUser(house.getHouseId(), clientUserBindHouse.getUserId(), clientUserBindHouse.getHouseUsertype());
            houseUserMapper.insert(houseUser);
        return MessageUtil.success("绑定成功");
    }
}
